package com.cssy.xxkj.logic.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cssy.authority.dao.AppSqlConfig;
import com.cssy.authority.service.IAuthorityService;
import com.cssy.authority.service.IScUserRolewhPlaceService;
import com.cssy.authority.service.impl.AuthorityServiceImpl;
import com.cssy.base.web.BaseController;
import com.cssy.base.web.BodyMsg;
import com.cssy.xxkj.logic.contants.BillStatusUtil;
import com.cssy.xxkj.logic.entity.LogicWarehouseOtherinDO;
import com.cssy.xxkj.logic.entity.LogicWarehouseOtherinItemDO;
import com.cssy.xxkj.logic.service.ILogicWareHouseOtherInItemService;
import com.cssy.xxkj.logic.service.ILogicWareHouseOtherInService;
import com.cssy.xxkj.logic.util.OftenMapUtil;
import com.cssy.xxkj.logic.util.OftenMothedUtil;
import com.cssy.yd.logic.util.SqlUtil;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.plat.exception.SyBaseException;
import com.plat.helper.JqGridHandler;
import com.plat.helper.SearchConditions;
import com.plat.helper.SearchResult;
import com.plat.util.GlobalResLoader;
import com.plat.util.OperationFlag;
import com.plat.util.SessionUtil;
import com.plat.util.json.OutJsonGrid;

@Controller
@RequestMapping("/logic/otherInwh")
public class OtherInController extends BaseController<LogicWarehouseOtherinDO>{

	private static final String MANAGER = "/xxkj/logic/otherinwh/Manager";
	private static final String EIDT = "/xxkj/logic/otherinwh/Operation";//添加、修改、显示页面
	@Resource(name = "logicWareHouseOtherInService")
	private ILogicWareHouseOtherInService logicWareHouseOtherInService;
	
	@Resource(name = "logicWareHouseOtherInItemService")
	private ILogicWareHouseOtherInItemService logicWareHouseOtherInItemService;
	
	@Resource(name="scUserRolePlaceService")
	private IScUserRolewhPlaceService scUserRolePlaceService;
	
	@Resource(name = "oftenMothedUtil")
	private OftenMothedUtil oftenMothedUtil;
	
	@Resource(name = "oftenMapUtil")
	private OftenMapUtil oftenMapUtil;

	@Override
	public String manager(HttpServletRequest request, Model model)
			throws Exception {
		model.addAttribute("wh",SqlUtil.sqlUtil.roleWH(request, scUserRolePlaceService));
		return MANAGER;
	}

	@Override
	public void list(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		JqGridHandler hander=new JqGridHandler(request);
		SearchConditions scs = new SearchConditions();
		hander.getWheres(scs);
		scs=this.oftenMothedUtil.getNoFinishConditions(scs, "zd003BillStatus", BillStatusUtil.OTHERWH_BILL_STATUE_FOUR);
		Map<String,Object> sqlMap = SqlUtil.sqlUtil.roleWhSqlFilter(request, scUserRolePlaceService,"zd004WhCode");
		scs.addCondition((String)sqlMap.get("option"), (List)sqlMap.get("roleWhList"));
		
		if(scs.getOrderBy()==null||"".equals(scs.getOrderBy())){
			scs.setOrderBy("zd010OtherDate desc, zd001InCode desc ");
		}
		SearchResult result= logicWareHouseOtherInService.findListByExample(scs);
		OutJsonGrid out=new OutJsonGrid(result);
		out.printJSon(response);
	}
	
	@Override
	public String operationForm(HttpServletRequest request,@PathVariable String id,
			@PathVariable  String flag, Model model) throws Exception {
		model.addAttribute("wh",SqlUtil.sqlUtil.roleWH(request, scUserRolePlaceService));
		if(OperationFlag.ADD.equals(flag)){//我新增加时操作
			model.addAttribute("viewflag", "add");
			model.addAttribute("xx", 1);//标示能不能修改内容
		}else{
			LogicWarehouseOtherinDO oi = logicWareHouseOtherInService.findListById(id);
			if (oi.getZd003BillStatus().equals(BillStatusUtil.OTHERWH_BILL_STATUS_ONEW)||oi.getZd003BillStatus().equals(BillStatusUtil.OTHERWH_BILL_STATUE_THREE)){
				model.addAttribute("xx", 1);//标示能不能修改内容
				model.addAttribute("viewflag", "tijiao");
			}else {
				model.addAttribute("xx", 2);//标示能不能修改内容
			}
			if (oi.getZd003BillStatus().equals(BillStatusUtil.OTHERWH_BILL_STATUE_FOUR)){
				model.addAttribute("viewflag", "tongguo");
			}
			if (oi.getZd003BillStatus().equals(BillStatusUtil.OTHERWH_BILL_STATUE_TWO)){
				model.addAttribute("viewflag", "shenpi");
			}
			model.addAttribute("oi", oi);
			
			if(oi!=null&&StringUtils.isNotBlank(oi.getZd006Aegnt())){
				SqlMapClient sqlMapClient = AppSqlConfig.getSqlMapInstance();
				IAuthorityService ias = new AuthorityServiceImpl(sqlMapClient);
				com.cssy.authority.entity.User u = ias.getByUserName(oi.getZd006Aegnt());
				model.addAttribute("userName", u.getRealname());
			}
		}
		return EIDT;
	}

	@RequestMapping("/griditems")
	public void griditems(HttpServletRequest request,HttpServletResponse response) throws Exception{
		String id = request.getParameter("id");
		if(StringUtils.isNotBlank(id)){
			List<LogicWarehouseOtherinItemDO> loiilist = logicWareHouseOtherInItemService.findByPid(id);
			OutJsonGrid out = new OutJsonGrid(loiilist);
			out.printJSon(response);
		}
	}
	
	@Override
	public Map<String, Object> save(HttpServletRequest request,
			LogicWarehouseOtherinDO curAP, BindingResult result, Model model)
			throws Exception {
		BodyMsg bodyMap = BodyMsg.getInstance();
		String itemdata = request.getParameter("addressInfos");
		if(itemdata==null&&"".equals(itemdata)){
			bodyMap.isFail();
			bodyMap.configWebInfor("物料不能为空！");
			return bodyMap.modelMap;
		}
		try {
			logicWareHouseOtherInService.coSave(curAP, request);
			bodyMap.isSuccess();
			bodyMap.configWebInfor(GlobalResLoader.getOpsuccess());
		} catch (SyBaseException e) {
			e.printStackTrace();
			bodyMap.isFail();
			bodyMap.configWebInfor(GlobalResLoader.getOpfailed()
					+ e.getMessage());
		}
		return bodyMap.modelMap;
	}

	@Override
	public Map<String, Object> delete(HttpServletRequest request,
			HttpServletResponse response, Model model) throws Exception {
		BodyMsg bodyMap=BodyMsg.getInstance();
		try{
			String ids = request.getParameter("ids");
			String[] fields=ids.split(",");
			for(String field : fields){
				LogicWarehouseOtherinDO lot=logicWareHouseOtherInService.findListById(field);
				if(lot.getId()!=null){
					lot.setRemoved(1l);
					logicWareHouseOtherInService.update(lot, SessionUtil.getUser(request));
				}
			}
		
			bodyMap.isSuccess(GlobalResLoader.getOpsuccess());
		}catch(Exception e){
			e.printStackTrace();
			bodyMap.isFail(GlobalResLoader.getOpfailed()+e.getMessage());
		}
		return bodyMap.modelMap;
	}

	@RequestMapping(value = "/updateState")
	@ResponseBody
	public Map<String,String> updateState(HttpServletRequest request,
			HttpServletResponse response, Model model) throws Exception {
		Map<String,String> map=new HashMap<String,String>();
		try{
			String ids = request.getParameter("rows");
			if(ids!=null&&!"".equals(ids)){
				LogicWarehouseOtherinDO lot=logicWareHouseOtherInService.findListById(ids);
				if(lot.getId()!=null){
					lot.setZd003BillStatus(BillStatusUtil.OTHERWH_BILL_STATUE_THREE);
					logicWareHouseOtherInService.update(lot, SessionUtil.getUser(request));
					map.put("msg", "SUCCESS");
				}
			}
		}catch(Exception e){
			e.printStackTrace();
			map.put("msg", "ERROR");
		}
		return map;
	}
	
}
